package com.javaspirit.android.diary.dal;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

/**
 * Diary Content Provider
 * 
 * @author CONVHD
 *
 */
public class DiaryContentProvider extends ContentProvider{

	private static final String TAG = "DiaryContentProvider";
	
	//Authority
	private static final String AUTHORITY = "com.javaspirit.android.diary.provider";
	
	// database
	private DbHelper dbHelper;
	private SQLiteDatabase db;
	
	
	/**
	 * Content URI for the NOTE Table
	 * Content URI is composed of an SCHEME(content://) + AUTHORITY + PATH(table)
	 */
	private static final String CONTENT_SCHEME = "content://";
	public static final Uri CONTENT_URI_NOTE = 
			Uri.parse( CONTENT_SCHEME + AUTHORITY + "/" + NoteTable.TABLE_NAME);

	
	// URI Matcher Stuff
	private static final int NOTE = 1;
	private static final int NOTE_ID = 2;
	private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
	static {
		sURIMatcher.addURI(AUTHORITY, NoteTable.TABLE_NAME, NOTE);
		sURIMatcher.addURI(AUTHORITY, NoteTable.TABLE_NAME + "/#", NOTE_ID);
	}
	
	

	
	
	

	@Override
	public boolean onCreate() {
		dbHelper = new DbHelper(getContext());
		db = dbHelper.getWritableDatabase();
		return true;
	}
	
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int uriType = sURIMatcher.match(uri);
		int rowsDeleted = 0;
		switch (uriType) {
		case NOTE:
			rowsDeleted = db.delete(NoteTable.TABLE_NAME, selection, selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return rowsDeleted;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		int uriType = sURIMatcher.match(uri);
		long id = 0;
		switch (uriType) {
		case NOTE:
			id = db.insert(NoteTable.TABLE_NAME, null, values);
			Log.i(TAG, "inserted a record:"+id);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return ContentUris.withAppendedId(CONTENT_URI_NOTE,id);
	}


	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		
		Cursor cursor = null;
		int uriType = sURIMatcher.match(uri);
		switch (uriType) {
		case NOTE:
			cursor = db.query(NoteTable.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
		
		return cursor;
		
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		int uriType = sURIMatcher.match(uri);
		int rowsUpdated = 0;
		switch (uriType) {
		case NOTE:
			rowsUpdated = db.update(NoteTable.TABLE_NAME, values, selection, selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return rowsUpdated;
	}
	

}
